package com.youlai.system.controller;

import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.DepartmentForm;
import com.youlai.system.model.form.PermissionForm;
import com.youlai.system.model.vo.DepartmentVO;
import com.youlai.system.model.vo.PermissionVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "10.我的权限接口")
@RestController
@RequestMapping("/api/v1/permission")
@RequiredArgsConstructor
public class PermissionController {

    private final PermissionService permissionService;

    @Operation(summary = "获取权限列表")
    @GetMapping("/permission")
    public Result<List<PermissionVO>> listPermissions() {
        List<PermissionVO> list = permissionService.listPermissions();
        return Result.success(list);
    }

    @Operation(summary = "新增或修改权限点")
    @PostMapping
    @PreventDuplicateSubmit
    public Result savePermission(
            @Valid @RequestBody PermissionForm formData
    ) {
        Integer id = permissionService.savePermission(formData);
        return Result.success(id);
    }

    @Operation(summary = "删除权限点")
    @DeleteMapping("/{ids}")
    public Result deletePermissions(
            @Parameter(description ="权限点ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids
    ) {
        boolean result = permissionService.deleteByIds(ids);
        return Result.judge(result);
    }

    @Operation(summary = "权限表单数据")
    @GetMapping("/{id}/form")
    public Result<PermissionForm> getPermissionForm(
            @Parameter(description = "权限点id") @PathVariable Long id
    ) {
        PermissionForm permissionForm = permissionService.getPermissionForm(id);
        return Result.success(permissionForm);
    }
}
